import DataManager from "../manager/DataManager";
import { privacyChannel } from "./privacyChannel";
import { privacyChannelOppo } from "./privacyChannelOppo";
import { privacyChannelVivo } from "./privacyChannelVivo";
import { prv } from "./prv";


const { ccclass, property } = cc._decorator;

@ccclass
export default class kuangDian extends cc.Component {

    @property({ type: cc.Node, tooltip: "nextShow" })
    nextShow = null;
    @property({ type: cc.Node, tooltip: "bt_next" })
    bt_next = null;
    @property({ type: cc.Node, tooltip: "狂点" })
    bt1 = null;
    @property({ type: cc.Node, tooltip: "狂点" })
    tip = null;
    // @property({ type: cc.Node, tooltip: "关闭" })
    // bt_close = null
    @property({ type: cc.Node, tooltip: "box" })
    box = null;
    @property({ type: cc.ProgressBar, tooltip: "progress" })
    progressBar = null;
    @property(cc.Node)
    down: cc.Node = null;

    isDone = false;
    isIconShow = false;
    // adMode: number = 0;


    start() {
        console.error("狂点 出现")
        this.progressBar.progress = 0.5;
        // this.bt_close.active = false;
        this.bt1.active = true;
        this.tip.active = true;
        this.isIconShow = false;
        this.bt1.on(cc.Node.EventType.TOUCH_END, () => {
            if (!this.isDone) {
                this.progressBar.progress += 0.12;
                return;
            }
            this.progressBar.progress = 1;
        }, this);
        this.nextShow.active = false;
        cc.tween(this.box).repeatForever(cc.tween().to(0.3, { scale: 1.2 }).to(0.3, { scale: 1 })).start();
        this.bt_next.active = false;
        if (true) {
            console.error("kuangDianShowInter")
            this.scheduleOnce(() => {
                let style = {
                    top: prv.getSysInfo().windowHeight / 2 + 270
                };
                prv.closeInter()
                switch (privacyChannel.channel) {
                    case "xm":
                        prv.showInter(false)
                        break
                    default:
                        prv.showInter(false, style);
                        break
                }

            }, 0.3)

        }
        switch (privacyChannel.channel) {
            case "vivo":
            case "vivo1":
                (privacyChannel.getChannel() as privacyChannelVivo).hasShortcutInstalled((hasShortcut) => {
                    if (!hasShortcut) {
                        (privacyChannel.getChannel() as privacyChannelVivo).installShortcut()
                    }
                });
                break
        }

    }

    showNext() {

        // prv.showVideo(()=>{
        //     this.hide();
        // });

        //插屏
        let rate = 100;
        let rando = Math.random() * 100;
        console.error("概率: ", rate, rando);
        if (rate > rando) {
            // let style = {
            //     top: prv.getSysInfo().windowHeight / 2 + 325
            // };
            //prv.showInter(false, style);
            privacyChannel.getChannel().showVideo(() => {
                DataManager.instance.power += 1
                DataManager.instance.save()
                this.node.destroy()
            })
            this.scheduleOnce(() => {
                if (cc.find("Canvas/UI/WinLayer").active || cc.find("Canvas/UI/LoseLayer").active) {
                    privacyChannel.getChannel().showInter(true)
                } else {
                    let style = {
                        top: prv.getSysInfo().windowHeight / 2 + 270
                    };

                    switch (privacyChannel.channel) {
                        case "oppo":
                        case "oppo1":

                            (privacyChannel.getChannel() as privacyChannelOppo).showInterMainLayer(true, style)
                            break
                        case "vivo":
                        case "vivo1":
                            privacyChannel.getChannel().showInter(true, style)
                            break
                        case "xm":
                            privacyChannel.getChannel().showInter()
                            break
                    }
                    //prv.showInter(false, style);
                    // (privacyChannel.getChannel() as privacyChannelOppo).showInterMainLayer(true, style)
                }

                this.node.destroy()
            }, 0.1)


        }
    }

    update() {
        if (this.isDone || this.progressBar.progress <= 0) {
            return;
        }
        if (this.progressBar.progress >= (0.55 + Math.random() * 0.25) && !this.isIconShow) {
            this.isIconShow = true;
            this.showNext();
        } else if (this.progressBar.progress >= 1) {
            this.isDone = true;
            // this.bt1Txt.string = '关  闭';
            this.box.pauseAllActions();
            // this.bt_close.active = true;
            this.bt1.active = false;
            this.tip.active = false;
            this.hide();
            return;
        }
        this.progressBar.progress -= 0.0015;
    }
    hide() {
        if (!this.node || !this.node.isValid) {
            return;
        }
        prv.closeInter();
        prv.closeKuangDian();
        // if (privacyChannel.getChannel().getCtrl("p3") == 1) {
        //     let style = {
        //         top: prv.getSysInfo().windowHeight / 2 + 200
        //     };
        //     prv.showInter(false, style);
        // }

        let style = {
            top: prv.getSysInfo().windowHeight / 2 + 200
        };
        prv.showInter(false, style);

        // if (privacyChannel.getChannel().getCtrl("p10") == 1) {
        //     let style = {
        //         top: privacyChannel.getChannel().getSysInfo().windowHeight / 2 + 100
        //     };
        //     this.scheduleOnce(() => {
        //         privacyChannel.getChannel().showInter(false, style)

        //     }, privacyChannel.getChannel().getCtrl("p11"))

        // }
        this.node.destroy();
    }
    // init() {
    //     this.adMode = Math.random() < 0.5 ? 1 : 2;
    //     // if(sznChannel.instance().channel != 'vivo'){
    //     //     if(this.adMode == 1){//底部banner
    //     //         this.down.getComponent(cc.Widget).bottom = 100;
    //     //         this.down.getComponent(cc.Widget).updateAlignment();
    //     //     }else{
    //     //         this.down.getComponent(cc.Widget).bottom = 300;
    //     //         this.down.getComponent(cc.Widget).updateAlignment();
    //     //     }
    //     // }
    // }



}
